package grex;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

import java.awt.Frame;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.StringTokenizer;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
//import javax.swing.filechooser.*;
public class FileManagerFidelity {
  private BufferedReader in;
  private ArrayList names = new ArrayList();
  Frame parent;
  public FileManagerFidelity(Frame parent) {
    this.parent = parent;
    init();
  }

  public void init() {
    JFileChooser jf = new JFileChooser();
    jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    if (JFileChooser.APPROVE_OPTION == jf.showOpenDialog(parent)) {
      File f = jf.getSelectedFile();
      File[] files = f.listFiles();
      StringTokenizer st;
      Object o;
      for (int i = 0; i < files.length; i++) {
        st = new StringTokenizer(files[i].getName(), ".");
        st.nextToken();
        if (st.hasMoreTokens() && st.nextToken().equalsIgnoreCase("batch"))
        names.add(files[i]);
      }
      Collections.sort(names);
      saveFile();
    }

  }
  private void saveFile(){
        JFileChooser jf = new JFileChooser("Skriv namnet som filerna skall sammanstollas till");
        if(JFileChooser.APPROVE_OPTION == jf.showSaveDialog(parent)){
          try {
            PrintWriter p = new PrintWriter(new FileWriter(jf.getSelectedFile()));
            p.println("Name\tTrain\tVal\tTest\tLength");
            StringTokenizer st;
            for (Iterator i = names.iterator(); i.hasNext(); ) {
              File file = (File)i.next();
              StringTokenizer namn = new StringTokenizer(file.getName(),".");
              p.write(namn.nextToken()+"\t");
              BufferedReader r = new BufferedReader(new FileReader(file));
              String line="",s="",train="",val="",test="",length="";
              double max=Double.MIN_VALUE;
              while ( (line = r.readLine()) != null) {
                st = new StringTokenizer(line, " ");
                if (st.hasMoreTokens()) {
                  s = st.nextToken();
                  if (s.equals("Train:")) {
                    s=st.nextToken();
                    if (Double.parseDouble(s) > max) {
                      max=Double.parseDouble(s);
                      train = s;
                      st = new StringTokenizer(r.readLine(), " ");
                      st.nextToken();
                      val = st.nextToken();
                      st = new StringTokenizer(r.readLine(), " ");
                      st.nextToken();
                      test = st.nextToken();
                      st = new StringTokenizer(r.readLine(), " ");
                      st.nextToken();
                      length = st.nextToken();
                    }
                  }
                }
              }
              r.close();
              p.println(train + "\t" + val + "\t" + test + "\t" + length);
            }
              p.close();
              JOptionPane.showMessageDialog(parent,"The file was created.","Information",JOptionPane.INFORMATION_MESSAGE);
          }
          catch (Exception ex) {
            ex.printStackTrace();
          }

        }
  }
}
